﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Pub.Class;
using Abp.Collections.Extensions;
using Abp.AutoMapper;
using Abp.Application.Services;
using LeiliTech.@(Model.ConfigInfo.ModelFolderName).Dto;

namespace LeiliTech.@(Model.ConfigInfo.ModelFolderName)
{
    public class @(Model.ConfigInfo.EntityName)AppService : ApplicationService, I@(Model.ConfigInfo.EntityName)AppService
    {
        I@(Model.ConfigInfo.EntityName)Repository _baseRepository = null;
        string tenantID = Session2.Get(SessionKey.TenantID);
        public @(Model.ConfigInfo.EntityName)AppService(I@(Model.ConfigInfo.EntityName)Repository baseRepository)
        {
            _baseRepository = baseRepository;
        }

        /// <summary>
        /// 获取实体信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public @(Model.ConfigInfo.EntityName)Dto GetEntity(string id)
        {
            var entity = _baseRepository.FirstOrDefault(id);
            if(entity != null)
            {
                return entity.MapTo<@(Model.ConfigInfo.EntityName)Dto>();
            }
            return new @(Model.ConfigInfo.EntityName)Dto();
        }

        /// <summary>
        /// 分页获取信息
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public ResultSet<@(Model.ConfigInfo.EntityName)Dto> QueryPage(@(Model.ConfigInfo.EntityName)Input input)
        {
            ResultSet<@(Model.ConfigInfo.EntityName)Dto> result = new ResultSet<@(Model.ConfigInfo.EntityName)Dto>();
            try
            {
                var query = _baseRepository.GetAll().Where(p => p.IsDelete == 0 && p.TenantID == tenantID)
                    //.WhereIf(!string.IsNullOrWhiteSpace(input.Code), m => m.Code.Contains(input.Code))
                    //.WhereIf(!string.IsNullOrWhiteSpace(input.Name), s => s.Name.Contains(input.Name)); 
					;
                result.total = query.Count();
                result.rows = query.OrderBy(p => p.SortNo).Skip(input.Skip).Take(input.PageSize).MapTo<List<@(Model.ConfigInfo.EntityName)Dto>>();
            }
            catch (Exception ex) { }
            return result;
        }

        /// <summary>
        /// 保存数据
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public string SaveEntity(@(Model.ConfigInfo.EntityName)Dto input)
        {
            string result = "";
            try
            {
                if (string.IsNullOrEmpty(input.Id))
                {
                    var entity = ObjectMapper.Map<@(Model.ConfigInfo.EntityName)>(input);
                    entity.Id = GlobalUtil.GenGuidCode();
                    entity.CreateDate = DateTime.Now;
                    entity.CreateUser = Session2.Get(SessionKey.AccountID);
                    entity.IsDelete = 0;
                    entity.TenantID = tenantID;
                    result = _baseRepository.InsertAndGetId(entity);
                }
                else
                {
                    var entity = _baseRepository.FirstOrDefault(input.Id);
                    if(entity != null)
                    {
						input.IsDelete = entity.IsDelete;
                        input.CreateDate = entity.CreateDate;
                        input.CreateUser = entity.CreateUser;
                        input.UpdateDate = DateTime.Now;
                        input.UpdateUser = Session2.Get(SessionKey.AccountID);
                        input.TenantID = entity.TenantID;
                        entity = ObjectMapper.Map(input, entity);
                        var res = _baseRepository.Update(entity);
                        if (res != null)
                        {
                            result = res.Id;
                        }     
                    }
                }
            }
            catch (Exception ex)
            {
                result = "";
                throw ex;
            }
            return result;
        }


        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public int DeleteEntity(List<string> ids)
        {
            int res = 0;
            foreach (var id in ids)
            {
                var r = _baseRepository.Update(id,
                    t =>
                    {
                        t.IsDelete = 1;
                        t.UpdateDate = DateTime.Now;
                        t.UpdateUser = Session2.Get(SessionKey.AccountID);
                    });
                res += r != null ? 1 : 0;
            }
            return res;
        }
    }
}
